package com.jspgou.core.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.dialect.Ingres10Dialect;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.core.dao.LogDao;
import com.jspgou.core.entity.Log;
@Repository
public class LogDaoImpl extends HibernateBaseDao<Log, Integer>implements LogDao {

	//*按要求获取日志分页数据
	public Pagination getPage(Integer category, Long siteId, Long adminId,
			String title, String ip, int pageNo, int pageSize) {
		Finder finder=Finder.create("from Log bean where 1=1");
		if(category!=null){
			finder.append(" and bean.category=:category");
			finder.setParam("category", category);
		}
		if(siteId!=null){
			finder.append(" and bean.website.id=:siteId");
			finder.setParam("siteId", siteId);
		}
		if(adminId!=null){
			finder.append(" and bean.admin.id=:adminId");
			finder.setParam("adminId", adminId);
		}
		if(StringUtils.isNotBlank(title)){
			finder.append(" and bean.title like:title");
		    finder.setParam("title", "%"+title+"%");
		}
		/*if(title!=null&&!title.equals("")){
			finder.append(" and bean.title like '%"+title+"%'");
		}*/
		if(StringUtils.isNotBlank(ip)){
			finder.append(" and bean.ip like:ip");
			finder.setParam("ip",ip+"%");
		}
		finder.append(" order by bean.id desc");
		return find(finder, pageNo, pageSize);
	}

	public Log findById(Long id) {
		int adminId=id.intValue();
		Log entity=get(adminId);
		return entity;
	}

	public Log save(Log bean) {
		getSession().save(bean);
		return bean;
	}

	public Log deleteById(Long id) {
	    int adminId=id.intValue();
		Log bean =get(adminId);
		if(bean!=null){
			getSession().delete(bean);
		}
		return bean;
	}
	
	public int deleteBatch(Integer category,Long webSiteId,Date time){
		Finder f=Finder.create("delete Log bean where 1=1");
		if(category!=null){
			f.append(" and bean.category=:category");
			f.setParam("category", category);
		}
		if(webSiteId!=null){
			f.append(" and bean.website.id=:website");
			f.setParam("website", webSiteId);
		}
		if(time!=null){
			f.append(" and bean.time<=:time");
			f.setParam("time", time);
		}
		Query q=f.createQuery(getSession());
		return q.executeUpdate();
	}

	@Override
	protected Class<Log> getEntityClass() {
		
		return Log.class;
	}

}
